package com.yuyu.qingmangmall.member.controller;

import com.yuyu.common.exception.BigCodeEnume;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.R;
import com.yuyu.qingmangmall.member.entity.MemberEntity;
import com.yuyu.qingmangmall.member.exception.EmailExistException;
import com.yuyu.qingmangmall.member.exception.UsernameExistException;
import com.yuyu.qingmangmall.member.feign.CouponFeignService;
import com.yuyu.qingmangmall.member.service.MemberService;
import com.yuyu.qingmangmall.member.vo.MemberRegisterVo;
import com.yuyu.qingmangmall.member.vo.MemberLoginVo;
import com.yuyu.qingmangmall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 会员
 *
 * @author yuwengsheng
 * @email 3337002363@qq.com
 * @date 2021-05-31 17:43:57
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //远程调用
    @Autowired
    CouponFeignService couponFeignService;

    //测试远程调用
    @RequestMapping("/test")
    public R test(){

        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R coupontest = couponFeignService.Coupontest();

        return R.ok().put("member",memberEntity).put("coupons",coupontest.get("coupon"));
    }


    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo registerVo) {

        try {
            memberService.register(registerVo);
        }catch (UsernameExistException uee){
            return R.error(BigCodeEnume.USERNAME_EXIST_EXCEPTION.getCode(),BigCodeEnume.USERNAME_EXIST_EXCEPTION.getMessage());
        }catch (EmailExistException eee){
            return R.error(BigCodeEnume.EMAIL_EXIST_EXCEPTION.getCode(),BigCodeEnume.EMAIL_EXIST_EXCEPTION.getMessage());
        }

        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {


        MemberEntity member = memberService.login(memberLoginVo);
        if (member!=null){
            // 成功拿到用户信息
            return R.ok().put("member",member);
        }else {
            return R.error(BigCodeEnume.ACCOUNT_OR_PASSWORD_EXCEPTION.getCode(),BigCodeEnume.ACCOUNT_OR_PASSWORD_EXCEPTION.getMessage());
        }

    }

    /**
     * 使用第三方软件登录
     * @param socialUser
     * @return
     */
    @PostMapping("/oauth/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) throws Exception {


        MemberEntity member = memberService.oauthLogin(socialUser);
        if (member!=null){
            // 成功拿到用户信息
            // TODO 登陆成功处理
            return R.ok().put("member",member);
        }else {
            return R.error(BigCodeEnume.ACCOUNT_OR_PASSWORD_EXCEPTION.getCode(),BigCodeEnume.ACCOUNT_OR_PASSWORD_EXCEPTION.getMessage());
        }

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
